package com.wu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.mapper.ReportLabelMapper;
import com.wu.pojo.ReportLabel;
import com.wu.pojo.model.ReportLabelModel;
import com.wu.service.ReportLabelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-12 13:54
 */
@Service
public class ReportLabelServiceImpl extends ServiceImpl<ReportLabelMapper, ReportLabel> implements ReportLabelService {

    @Autowired
    private ReportLabelMapper reportLabelMapper;

    @Override
    public List<ReportLabelModel> getAllReportLabel() {
        //1、获取所有的父级标签
        List<ReportLabel> parentLabel = reportLabelMapper.getAllReportLabel();
        //2、获取所有的子级标签
        List<ReportLabel> childLabel = reportLabelMapper.getAllChildReportLabel();
        return parentLabel.stream().map(parent -> {
            ReportLabelModel parentLabelModel = new ReportLabelModel();
            BeanUtils.copyProperties(parent, parentLabelModel);

            ArrayList<ReportLabelModel> childLabelList = new ArrayList<>();
            for (ReportLabel child : childLabel) {
                ReportLabelModel childLabelModel = new ReportLabelModel();
                BeanUtils.copyProperties(child, childLabelModel);
                if (parentLabelModel.getReportId().equals(childLabelModel.getReportParentId())) {
                    childLabelList.add(childLabelModel);
                }
            }
            parentLabelModel.setChildLabel(childLabelList);
            return parentLabelModel;
        }).collect(Collectors.toList());
    }
}
